package com.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by
 *
 * @author touni
 * @date 2019/4/11
 * @time 18:14
 */
@RestController
@DefaultProperties(defaultFallback = "defaultFallbackMethod")
public class DemoController {

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/user/{id}")
    @HystrixCommand(commandProperties = @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")) //此处更改2s触发服务降级
    public Object transferFromService(@PathVariable Integer id){


        User user = restTemplate.getForObject("http://user-service/user/" + id, User.class);

        return user;

    }



    public Object defaultFallbackMethod(){

        return "您的网络有问题，请检查后重试！！";

    }








}
